package com.imm.da.main.city

import android.content.Context
import android.util.Log
import com.imm.da.base.vm.BaseViewModel
import com.imm.da.utils.LogHelper
import com.qweather.sdk.bean.base.Lang
import com.qweather.sdk.bean.base.Range
import com.qweather.sdk.bean.geo.GeoBean
import com.qweather.sdk.view.QWeather
import com.qweather.sdk.view.QWeather.OnResultGeoListener

class CityListViewModel: BaseViewModel() {


    fun queryHotCity(
        context: Context,
        success: (bean: List<GeoBean.LocationBean>) -> Unit,
        failure: () -> Unit = {  }
    ){
        QWeather.getGeoTopCity(context, 2, Range.CN, Lang.ZH_HANS, object : OnResultGeoListener {
            override fun onError(t: Throwable?) {
                Log.e("aaaaa", "queryHotCity, error: $t")
                failure()
            }

            override fun onSuccess(bean: GeoBean?) {
                Log.e("aaaaa", "queryHotCity, bean: ${bean?.code}")
                Log.e("aaaaa", "queryHotCity, bean: ${bean?.locationBean}")
                bean?.locationBean?.let { success }?: LogHelper.e("queryHotCity, result list can not be null")
            }
        })
    }


    fun queryCity(
        context: Context,
        success: (bean: List<GeoBean.LocationBean>) -> Unit,
        failure: () -> Unit = {  }
    ){
        QWeather.getGeoCityLookup(context, "杭州", "浙江", Range.CN, 2, Lang.ZH_HANS, object : OnResultGeoListener {
            override fun onError(t: Throwable?) {
                Log.e("aaaaa", "queryCity, error: $t")
                failure()
            }
            override fun onSuccess(bean: GeoBean?) {
                Log.e("aaaaa", "queryCity, bean: ${bean?.code}")
                Log.e("aaaaa", "queryCity, bean: ${bean?.locationBean}")
                bean?.locationBean?.let { success }?: LogHelper.e("queryCity, result list can not be null")
            }
        })
    }


}